//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.ComponentModel;
using System.Workflow.ComponentModel;

namespace Microsoft.Research.ScientificWorkflow.Activities
{
    [Name("Write to Console")]
    [Description("Writes text passed to it to the console. The text can be written to the standard output console or the error console.")]
    [WorkflowCategory("Data")]
    public class WriteToConsole : Activity
	{

        #region Dependency Properties

		public static DependencyProperty InputTextProperty = DependencyProperty.Register("InputText", 
            typeof(string), typeof(WriteToConsole), new PropertyMetadata(string.Empty));

        [RequiredInputParam]
        [Name("Input Text", "Text")]
        [Description("The text to write to the console.")]
		public string InputText
		{
			get
			{
				return ((string)(base.GetValue(WriteToConsole.InputTextProperty)));
			}
			set
			{
				base.SetValue(WriteToConsole.InputTextProperty, value);
			}
		}


        public static DependencyProperty WriteToStandardOutProperty = DependencyProperty.Register("WriteToStandardOut",
            typeof(bool), typeof(WriteToConsole), new PropertyMetadata(true));

        [OptionalInputParam]
        [Name("Write to Standard Out", "Write to Out")]
        [Description("Indicates whether or not the input text should be written to the standard output stream.")]
        public bool WriteToStandardOut
        {
            get
            {
                return ((bool)(base.GetValue(WriteToConsole.WriteToStandardOutProperty)));
            }
            set
            {
                base.SetValue(WriteToConsole.WriteToStandardOutProperty, value);
            }
        }

        public static DependencyProperty WriteToStandardErrProperty = DependencyProperty.Register("WriteToStandardErr",
            typeof(bool), typeof(WriteToConsole), new PropertyMetadata(true));

        [OptionalInputParam]
        [Name("Write to Error", "Write to Err")]
        [Description("Indicates whether or not the input text should be written to the standard error output stream.")]
        public bool WriteToStandardErr
        {
            get
            {
                return ((bool)(base.GetValue(WriteToConsole.WriteToStandardErrProperty)));
            }
            set
            {
                base.SetValue(WriteToConsole.WriteToStandardErrProperty, value);
            }
        }

        #endregion

		protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
		{
            if (WriteToStandardOut)
                Console.Out.WriteLine(InputText);

            if (WriteToStandardErr)
                Console.Error.WriteLine(InputText);

            return ActivityExecutionStatus.Closed;
		}
	}
}
